import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 导入nprogress加载进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 路由信息数组
const routes = [
  // 首页
  {
    path: '/',
    // 设置路由元信息
    meta: {
      title: "Bing酒店管理"
    },
    component: () => import('@v/Index.vue')
  },
  // 登录页
  {
    path: '/login',
    meta: {
      title: "登录"
    },
    component: () => import('@v/Login.vue')
  },
  // 后台管理页面
  {
    path: '/layout',
    meta: {
      title: "Bing酒店管理"
    },
    component: () => import('@v/Layout.vue'),
    // 配置嵌套路由
    children: [
      // 首页
      {
        path: '',
        meta: {
          title: "首页",
        },
        component: () => import('@v/user/Home.vue')
      },
      {
        path: 'message',
        meta: {
          title: "系统消息",
        },
        component: () => import('@v/user/Message.vue')
      },
      {
        path: 'email',
        meta: {
          title: "电子邮箱",
        },
        component: () => import('@v/user/Email.vue')
      },
      {
        path: 'mine',
        meta: {
          title: "个人中心",
        },
        component: () => import('@v/user/Mine.vue')
      },
      {
        path: 'resetPwd',
        meta: {
          title: "修改密码",
        },
        component: () => import('@v/user/ResetPwd.vue')
      },

      {
        path: 'role',
        meta: {
          title: "角色管理"
        },
        component: () => import('@v/role/Role.vue'),
      },
      {
        path: 'admin',
        meta: {
          title: "账户管理"
        },
        component: () => import('@v/admin/Admin.vue'),
      },
      {
        path: 'roomType',
        meta: {
          title: "客房类型管理"
        },
        component: () => import('@v/roomType/RoomType.vue'),
      },
      {
        path: 'room',
        meta: {
          title: "房间管理"
        },
        component: () => import('@v/room/Room.vue'),
      },
      {
        path: 'guest',
        meta: {
          title: "客户管理"
        },
        component: () => import('@v/guest/Guest.vue'),
      },
      {
        path: 'permission',
        meta: {
          title: "权限管理"
        },
        component: () => import('@v/permission/Permission.vue'),
      },
    ]
  },
  {
    // *号表示通过一层一层路由查找后,剩余的所有路由
    path: "*",
    component: () => import('@v/Error404.vue'),
  }
]

// 创建路由对象
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 开启进度条
  NProgress.start();
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  next();
})

// 路由前置守卫
router.afterEach((to, from) => {
  // 关闭进度条
  NProgress.done();
})

export default router